package at.jsug.topicvoting.gwt1.client.vo;

public class Topic  implements Comparable<Topic> {

	private final TopicState state;
	
	private final long id;
		
	private final String title;
	
	private final int votes;
	
	private final boolean yetVoted;
	

	private Topic(TopicState state, long id, String title, int votes, boolean yetVoted) {
		if(state == null) throw new IllegalArgumentException("state == null");
		if(title == null) throw new IllegalArgumentException("title == null");
		if(votes < 0) throw new IllegalArgumentException("votes < 0");
		
		this.state = state;
		this.id = id;
		this.title = title;
		this.votes = votes;
		this.yetVoted = yetVoted;
	}
	
	public static Topic newNeedTopic(long id, String title, int votes, boolean yetVoted) {
		return new Topic(TopicState.NEED, id, title, votes, yetVoted);
	}
	
	public int hashCode() {
		return ((int)id) + title.hashCode();
	}
	
	public boolean equals(Object o) {
		if(o == null) return false;
		if(o.getClass().getName().equals(this.getClass().getName()) == false) {
			return false;
		}
		final Topic that = (Topic) o;
		return
			this.id == that.id &&
			this.title.equals(that.title) &&
			this.votes == that.votes &&
			this.yetVoted == that.yetVoted;
	}

	public int compareTo(Topic that) {
		final int titleCompared = this.title.compareTo(that.title);
		if(titleCompared != 0) return titleCompared;
		return (this.id < that.id) ? -1 : 1;
	}
	
	public String toString() {
		return "Topic[id="+id+";title=["+title+"];votes="+votes+";yetVoted="+yetVoted+"]";
	}
	/*
	public final String toJSON() {          
        return new JSONObject(this).toString();
	}
	 */
	public TopicState getState() {
		return state;
	}

	public long getId() {
		return id;
	}
	
	public String getTitle() {
		return title;
	}

	public int getVotes() {
		return votes;
	}

	public boolean isYetVoted() {
		return yetVoted;
	}

}
